﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_v01
{
   public partial class AddEditQuestion : Form
   {
      private ArrayList tempPossible;
      private ArrayList tempCorrect;
      private bool mode;
      private QuestionDetail existingQuestion;

      public AddEditQuestion(bool addMode)
      {
         InitializeComponent();

         tempPossible = new ArrayList();
         tempCorrect = new ArrayList();
         mode = addMode;

         if (addMode)
         {
            okButton.Enabled = false;
         }
         else
         {
            okButton.Enabled = true;
         }
      }

      private void addButton_Click(object sender, EventArgs e)
      {
         string answer = possibleAnswerTextBox.Text;
         if ((answer != "") && (!CheckExists(answer)))
         {
            answerCheckedListBox.Items.Add(answer);
            tempPossible.Add(answer);
            possibleAnswerTextBox.Text = "";
         }
      }

      private void removeButton_Click(object sender, EventArgs e)
      {
         int size = answerCheckedListBox.CheckedItems.Count;
         int indicesSize = answerCheckedListBox.CheckedIndices.Count;
         string[] checkedItems = new string[size];
         int[] checkedIndices = new int[indicesSize];
         for (int i = 0; i < size; i++)
         {
            checkedIndices[i] = answerCheckedListBox.CheckedIndices[i];
         }

         int offset = 0;
         for (int i = 0; i < indicesSize; i++)
         {
            int removeIndex = checkedIndices[i] - offset;
            answerCheckedListBox.Items.RemoveAt(removeIndex);
            tempPossible.RemoveAt(removeIndex);

            tempCorrect.Remove(checkedIndices[i]);

            offset++;
         }
      }

      private void setCorrectButton_Click(object sender, EventArgs e)
      {
         tempCorrect = new ArrayList();

         //foreach (int checkedIndex in answerCheckedListBox.CheckedIndices)
         //{
         //   tempCorrect.Add(checkedIndex);

         //   if (!answerCheckedListBox.Items[checkedIndex].ToString().Contains("(Correct Answer)"))
         //   {
         //      answerCheckedListBox.Items[checkedIndex] = "(Correct Answer) " + answerCheckedListBox.Items[checkedIndex].ToString();
         //   }
         //}
         int size = answerCheckedListBox.Items.Count;
         for (int i = 0; i < size; i++)
         {
            string listItem = answerCheckedListBox.Items[i].ToString();
            if (listItem.Contains("(Correct Answer) "))
            {
               string temp = listItem.Replace("(Correct Answer) ", "");
               answerCheckedListBox.Items[i] = temp;
            }
         }

         foreach (int checkedIndex in answerCheckedListBox.CheckedIndices)
         {
            string checkedItem = answerCheckedListBox.Items[checkedIndex].ToString();
            tempCorrect.Add(checkedItem);

            if (!checkedItem.Contains("(Correct Answer)"))
            {
               answerCheckedListBox.Items[checkedIndex] = "(Correct Answer) " + checkedItem;
            }
         }
      }

      private void okButton_Click(object sender, EventArgs e)
      {
         //foreach (int temp in tempCorrect)
         //{
         //   Console.WriteLine(temp);
         //}

         if (tabControl1.SelectedTab.Text == "Multiple Choice")
         {
            if (mcqQuestionTextBox.Text != "")
            {
               if (tempCorrect.Count > 0)
               {
                  RunMode();
               }
               else
               {
                  MessageBox.Show("Correct answer(s) have not been set.", "No correct answers",
                     MessageBoxButtons.OK, MessageBoxIcon.Stop);
               }
            }
            else
            {
               MessageBox.Show("Question has not been set.", "No question",
                     MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
         }
         else if (tabControl1.SelectedTab.Text == "Open")
         {
            if (openQuestionTextBox.Text != "")
            {
               RunMode();
            }
            else
            {
               MessageBox.Show("Question has not been set.", "No question",
                     MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
         }
         else if (tabControl1.SelectedTab.Text == "True/False")
         {
            if (tfQuestionTextBox.Text != "")
            {
               RunMode();
            }
            else
            {
               MessageBox.Show("Question has not been set.", "No question",
                     MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
         }
      }

      private void AddNewQuestion ()
      {
         QuestionDetail newQuestion = CreateQuestion();

         MainApp main = (MainApp)this.MdiParent;
         main.AddQuestion(newQuestion);
      }

      private void EditExistingQuestion()
      {
         QuestionDetail newQuestion = CreateQuestion();

         MainApp main = (MainApp)this.MdiParent;
         main.EditQuestion(newQuestion);
      }

      private QuestionDetail CreateQuestion()
      {
         QuestionDetail newQuestion = new QuestionDetail();

         if (tabControl1.SelectedTab.Text == "Multiple Choice")
         {
            newQuestion.Type = QuestionType.MCQ;
            newQuestion.Question = mcqQuestionTextBox.Text;
            newQuestion.PossibleAnswers = tempPossible;
            newQuestion.CorrectAnswers = tempCorrect;
         }
         else if (tabControl1.SelectedTab.Text == "Open")
         {
            newQuestion.Type = QuestionType.Open;
            newQuestion.Question = openQuestionTextBox.Text;

            ArrayList newCorrect = new ArrayList();
            newCorrect.Add(openAnswerTextBox.Text);
            newQuestion.PossibleAnswers = newCorrect;
            newQuestion.CorrectAnswers = newCorrect;
         }
         else if (tabControl1.SelectedTab.Text == "True/False")
         {
            newQuestion.Type = QuestionType.TFQ;
            newQuestion.Question = tfQuestionTextBox.Text;
            newQuestion.PossibleAnswers = QuestionDetail.CreateTFAnswers();
            if (falseRadioButton.Checked)
            {
               newQuestion.AddCorrect("False");
            }
            else
            {
               newQuestion.AddCorrect("True");
            }
         }
         else
         {
            newQuestion.Type = QuestionType.Open;
            newQuestion.Question = openQuestionTextBox.Text;

            ArrayList newCorrect = new ArrayList();
            newCorrect.Add(openAnswerTextBox.Text);
            newQuestion.CorrectAnswers = newCorrect;
         }

         return newQuestion;
      }

      public void LoadExistingQuestion()
      {
         QuestionType type = existingQuestion.Type;

         if (type == QuestionType.MCQ)
         {
            tabControl1.SelectTab(mcqTabPage);

            mcqQuestionTextBox.Text = existingQuestion.Question;
            tempPossible = existingQuestion.PossibleAnswers;
            tempCorrect = existingQuestion.CorrectAnswers;

            //Console.WriteLine(tempCorrect.Count);

            answerCheckedListBox.Items.Clear();
            foreach (string answer in tempPossible)
            {
               string temp = answer;

               foreach (string correct in tempCorrect)
               {
                  if (correct == temp)
                  {
                     temp = "(Correct Answer) " + temp;
                     break;
                  }
               }

               answerCheckedListBox.Items.Add(temp);
            }
         }
         else if (type == QuestionType.Open)
         {
            tabControl1.SelectTab(openTabPage);

            openQuestionTextBox.Text = existingQuestion.Question;
            tempCorrect = existingQuestion.CorrectAnswers;
            if (tempCorrect.Count > 0)
            {
               openAnswerTextBox.Text = tempCorrect[0].ToString();
            }
         }
         else if (type == QuestionType.TFQ)
         {
            tabControl1.SelectTab(tfTabPage);

            tfQuestionTextBox.Text = existingQuestion.Question;
            tempCorrect = existingQuestion.CorrectAnswers;
            if (tempCorrect[0].ToString() == "False")
            {
               falseRadioButton.Checked = true;
               trueRadioButton.Checked = false;
            }
            else
            {
               falseRadioButton.Checked = false;
               trueRadioButton.Checked = true;
            }
         }
         else
         {
            tabControl1.SelectTab("Open");

            openQuestionTextBox.Text = existingQuestion.Question;
         }
      }

      private void mcqQuestionTextBox_TextChanged(object sender, EventArgs e)
      {
         if (mcqQuestionTextBox.Text == "")
         {
            okButton.Enabled = false;
         }
         else
         {
            okButton.Enabled = true;
         }
      }

      private void openQuestionTextBox_TextChanged(object sender, EventArgs e)
      {
         if (openQuestionTextBox.Text == "")
         {
            okButton.Enabled = false;
         }
         else
         {
            okButton.Enabled = true;
         }
      }

      private void tfQuestionTextBox_TextChanged(object sender, EventArgs e)
      {
         if (tfQuestionTextBox.Text == "")
         {
            okButton.Enabled = false;
         }
         else
         {
            okButton.Enabled = true;
         }
      }

      private bool CheckExists(string newAnswer)
      {
         bool exists = false;

         foreach (string answer in tempPossible)
         {
            if (answer == newAnswer)
            {
               exists = true;
               break;
            }
         }

         return exists;
      }

      private void RunMode()
      {
         if (mode)
         {
            AddNewQuestion();
         }
         else
         {
            EditExistingQuestion();
         }

         this.Close();
      }

      public QuestionDetail ExistingQuestion
      {
         set
         {
            existingQuestion = value;
         }
      }

      private void AddEditQuestion_FormClosing(object sender, FormClosingEventArgs e)
      {
         MainApp main = (MainApp) this.MdiParent;
         main.ToggleEditLessonEnable();
      }

      private void cancelButton_Click(object sender, EventArgs e)
      {
         this.Close();
      }

      private void resetButton_Click(object sender, EventArgs e)
      {
         if (!mode)
         {
            LoadExistingQuestion();
         }
         else
         {
            mcqQuestionTextBox.Text = "";
            possibleAnswerTextBox.Text = "";
            answerCheckedListBox.Items.Clear();
            openQuestionTextBox.Text = "";
            openAnswerTextBox.Text = "";
            tfQuestionTextBox.Text = "";
            trueRadioButton.Checked = true;
            tempPossible = new ArrayList();
            tempCorrect = new ArrayList();
         }
      }
   }
}
